#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: WANDERSON M.PIMENTA
@Software: PyCharm 、Qt Designer and PySide6
@File: tQLeftMenuBar.py
@Time: 2024/05/11 16:35
@Function：ctQLeftMenuBar：左侧菜单栏
"""
from tQQtCore import *
from .tQLeftMenuButton import ctQLeftMenuButton
from .tQLeftMenuDivLin import ctQLeftMenuDivLin
from UI.cores.tFunctions import *


# 左侧菜单栏
class ctQLeftMenuBar(QWidget):
    # 自定义信号
    clicked = Signal(object)
    released = Signal(object)

    # 初始化
    def __init__(self,
                 parent=None,
                 app_parent=None,
                 dark_one="#1b1e23",
                 dark_three="#21252d",
                 dark_four="#272c36",
                 bg_one="#2c313c",
                 icon_color="#c3ccdf",
                 icon_color_hover="#dce1ec",
                 icon_color_pressed="#edf0f5",
                 icon_color_active="#f5f6f9",
                 context_color="#568af2",
                 text_foreground="#8a95aa",
                 text_active="#dce1ec",
                 duration_time=500,
                 radius=8,
                 minimum_width=50,
                 maximum_width=240,
                 icon_path="icon_menu.svg",
                 icon_path_close="icon_menu_close.svg",
                 toggle_text="Hide Menu",
                 toggle_tooltip="Show menu"
                 ):
        """
        左侧菜单栏(导航栏)
        """
        super().__init__()

        # <editor-fold desc="布局&框架">
        self.__m_layoutLeftMenu = None
        self.__m_layoutBg = None
        self.__m_layoutTop = None
        self.__m_layoutBottom = None
        self.__m_frameBg = None
        self.__m_frameTop = None
        self.__m_frameBottom = None
        # </editor-fold>

        # <editor-fold desc="属性">
        self.animation = None
        self._dark_one = dark_one
        self._dark_three = dark_three
        self._dark_four = dark_four
        self._bg_one = bg_one
        self._icon_color = icon_color
        self._icon_color_hover = icon_color_hover
        self._icon_color_pressed = icon_color_pressed
        self._icon_color_active = icon_color_active
        self._context_color = context_color
        self._text_foreground = text_foreground
        self._text_active = text_active
        self._duration_time = duration_time
        self._radius = radius
        self._minimum_width = minimum_width
        self._maximum_width = maximum_width
        self._icon_path = Functions.f_getSvgIconPath(icon_path)
        self._icon_path_close = Functions.f_getSvgIconPath(icon_path_close)
        # 设置主体
        self._parent = parent
        self._app_parent = app_parent
        # </editor-fold>

        # 左侧菜单布局函数
        self.__f_leftMenuLayoutFunc()

        # 背景框架设置样式表
        self.__m_frameBg.setStyleSheet(f"background: {dark_one}; border-radius: {radius};")

        # <editor-fold desc="顶部布局：切换开关和分割线">
        # 自定义左侧菜单栏：隐藏按键
        self.__m_btnToggle = ctQLeftMenuButton(app_parent,
                                               text=toggle_text,
                                               tooltip_text=toggle_tooltip,
                                               dark_one=self._dark_one,
                                               dark_three=self._dark_three,
                                               dark_four=self._dark_four,
                                               bg_one=self._bg_one,
                                               icon_color=self._icon_color,
                                               icon_color_hover=self._icon_color_active,
                                               icon_color_pressed=self._icon_color_pressed,
                                               icon_color_active=self._icon_color_active,
                                               context_color=self._context_color,
                                               text_foreground=self._text_foreground,
                                               text_active=self._text_active,
                                               icon_path=icon_path
                                               )
        self.__m_btnToggle.clicked.connect(self.__f_toggleAnimation)
        self.__m_divTop = ctQLeftMenuDivLin(dark_four)

        # 顶部布局添加切换开关(隐藏开关)和分割线
        self.__m_layoutTop.addWidget(self.__m_btnToggle)
        self.__m_layoutTop.addWidget(self.__m_divTop)
        # </editor-fold>

        # <editor-fold desc="底部布局：分割线">
        self.__m_divBottom = ctQLeftMenuDivLin(dark_four)
        self.__m_divBottom.hide()
        self.__m_layoutBottom.addWidget(self.__m_divBottom)
        # </editor-fold>

    # 左侧菜单栏添加按键函数
    def f_leftMenuAddButtons(self, parameters):
        """
        function:  左侧菜单栏添加按键函数
              in:  parameters
             out:  None
          return:  None
          others:  Left Menu Bar Add Buttons Func
        """
        if parameters is not None:
            for parameter in parameters:
                _btn_icon = parameter['btn_icon']
                _btn_id = parameter['btn_id']
                _btn_text = parameter['btn_text']
                _btn_tooltip = parameter['btn_tooltip']
                _show_top = parameter['show_top']
                _is_active = parameter['is_active']
                self.menu = ctQLeftMenuButton(self._app_parent,
                                              text=_btn_text,
                                              btn_id=_btn_id,
                                              tooltip_text=_btn_tooltip,
                                              dark_one=self._dark_one,
                                              dark_three=self._dark_three,
                                              dark_four=self._dark_four,
                                              bg_one=self._bg_one,
                                              icon_color=self._icon_color,
                                              icon_color_hover=self._icon_color_active,
                                              icon_color_pressed=self._icon_color_pressed,
                                              icon_color_active=self._icon_color_active,
                                              context_color=self._context_color,
                                              text_foreground=self._text_foreground,
                                              text_active=self._text_active,
                                              icon_path=_btn_icon,
                                              is_active=_is_active
                                              )
                self.menu.clicked.connect(self.__f_btnClicked)
                self.menu.released.connect(self.__f_btnReleased)

                # ADD TO LAYOUT
                if _show_top:
                    self.__m_layoutTop.addWidget(self.menu)
                else:
                    self.__m_divBottom.show()
                    self.__m_layoutBottom.addWidget(self.menu)

    # 选择一个页面
    def f_selectOnlyOnePage(self, widget: str):
        """
        function:  选择一个页面
              in:  widget：
             out:  None
          return:  None
          others:  Select Only One Page
        """
        for btn in self.findChildren(QPushButton):
            if btn.objectName() == widget:
                btn.f_setMenuActive(True)
            else:
                btn.f_setMenuActive(False)

    # 选择一个选项卡
    def f_selectOnlyOneTab(self, widget: str):
        """
        function:  选择一个选项卡(leftColumn.ui)
              in:  widget：
             out:  None
          return:  None
          others:  Select Only One Tab From leftColumn.ui
        """
        for btn in self.findChildren(QPushButton):
            if btn.objectName() == widget:
                btn.f_setTabActive(True)
            else:
                btn.f_setTabActive(False)

    # 取消所有按键的选择
    def f_deselectAll(self):
        """
        function:  取消所有按键的选择
              in:  None
             out:  None
          return:  None
          others:  Deselect All Buttons
        """
        for btn in self.findChildren(QPushButton):
            btn.f_setMenuActive(False)

    # 取消所有按键(选项卡)的选择
    def f_deselectAllTab(self):
        """
        function:  取消所有按键(选项卡)的选择
              in:  None
             out:  None
          return:  None
          others:  Deselect All Buttons(Tan)
        """
        for btn in self.findChildren(QPushButton):
            btn.f_setTabActive(False)

    # <editor-fold desc="该类用到的私有方法">
    # 左侧菜单布局函数
    def __f_leftMenuLayoutFunc(self):
        # 左侧菜单布局
        self.__m_layoutLeftMenu = QVBoxLayout(self)
        self.__m_layoutLeftMenu.setContentsMargins(0, 0, 0, 0)

        # 背景框架(background)
        self.__m_frameBg = QFrame()

        # 顶部框架
        self.__m_frameTop = QFrame()

        # 底部框架
        self.__m_frameBottom = QFrame()

        # 背景布局
        self.__m_layoutBg = QVBoxLayout(self.__m_frameBg)
        self.__m_layoutBg.setContentsMargins(0, 0, 0, 0)

        # 顶部布局
        self.__m_layoutTop = QVBoxLayout(self.__m_frameTop)
        self.__m_layoutTop.setContentsMargins(0, 0, 0, 0)
        self.__m_layoutTop.setSpacing(1)

        # 底部布局
        self.__m_layoutBottom = QVBoxLayout(self.__m_frameBottom)
        self.__m_layoutBottom.setContentsMargins(0, 0, 0, 8)
        self.__m_layoutBottom.setSpacing(1)

        # 背景布局添加顶部布局、底部布局
        self.__m_layoutBg.addWidget(self.__m_frameTop, 0, Qt.AlignTop)
        self.__m_layoutBg.addWidget(self.__m_frameBottom, 0, Qt.AlignBottom)

        # 左侧菜单布局添加背景布局
        self.__m_layoutLeftMenu.addWidget(self.__m_frameBg)

    # 隐藏按键切换动画
    def __f_toggleAnimation(self):
        # 创建动画
        self.animation = QPropertyAnimation(self._parent, b"minimumWidth")
        self.animation.stop()
        if self.width() == self._minimum_width:
            self.animation.setStartValue(self.width())
            self.animation.setEndValue(self._maximum_width)
            # 切换开关激活：打开
            self.__m_btnToggle.f_setToggleActive(True)
            # 设置为关闭icon
            self.__m_btnToggle.f_setIcon(self._icon_path_close)
        else:
            self.animation.setStartValue(self.width())
            self.animation.setEndValue(self._minimum_width)
            # 切换开关激活：关闭
            self.__m_btnToggle.f_setToggleActive(False)
            self.__m_btnToggle.f_setIcon(self._icon_path)
        self.animation.setEasingCurve(QEasingCurve.InOutCubic)
        self.animation.setDuration(self._duration_time)
        self.animation.start()

    # 左侧菜单按键点击时发送信号
    def __f_btnClicked(self):
        self.clicked.emit(self.menu)

    # 左侧菜单按键释放时发送信号
    def __f_btnReleased(self):
        self.released.emit(self.menu)
    # </editor-fold>
